package com.shengdong.water.code.widget

import android.content.Context
import android.util.AttributeSet
import android.content.res.TypedArray
import android.view.LayoutInflater
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.shengdong.water.R
import androidx.core.content.withStyledAttributes
import com.hjq.shape.layout.ShapeLinearLayout

class FilterDrownView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :ConstraintLayout(context, attrs, defStyleAttr) {

    private val filterView1: AppCompatTextView
    private val filterView2: AppCompatTextView
    private val filterView3: AppCompatTextView
    private val selectFilter1: ShapeLinearLayout
    private val selectFilter2: ShapeLinearLayout
    private val selectFilter3: ShapeLinearLayout

    private var onClickListener: OnClickListener? = null

    init {
        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.layout_filter_drown, this, true)
        // 获取控件引用
        filterView1 = findViewById(R.id.tv_filter1)
        filterView2 = findViewById(R.id.tv_filter2)
        filterView3 = findViewById(R.id.tv_filter3)

        selectFilter1 = findViewById(R.id.select_filter1)
        selectFilter2 = findViewById(R.id.select_filter2)
        selectFilter3 = findViewById(R.id.select_filter3)


        selectFilter1.setOnClickListener {
            onClickListener?.onClick(it)
        }
        selectFilter2.setOnClickListener {
            onClickListener?.onClick(it)
        }
        selectFilter3.setOnClickListener {
            onClickListener?.onClick(it)
        }


        // 设置默认高度
        val defaultHeight = resources.getDimensionPixelSize(R.dimen.dp40)

        layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, defaultHeight)

        // 读取自定义属性
       context.withStyledAttributes(attrs, R.styleable.FilterDrownView) {
           initAttributes(this)
       }


        // 设置点击事件

//        super.setOnClickListener {
//
//            onClickListener?.onClick(it)
//
//        }

    }


    private fun initAttributes(typedArray: TypedArray) {

        // 读取属性

        val filterOne = typedArray.getString(R.styleable.FilterDrownView_filter_one) ?: ""

        val filterTwo = typedArray.getString(R.styleable.FilterDrownView_filter_two) ?: ""

        val filterThree = typedArray.getString(R.styleable.FilterDrownView_filter_three) ?: ""

        filterView1.text = filterOne

        filterView2.text = filterTwo

        filterView3.text = filterThree

    }


    override fun setOnClickListener(listener: OnClickListener?) {

        onClickListener = listener

    }

}